class MysteryMerchantView {
    static show(character, content = null) {
        // 如果没有提供内容，从商品池随机选择
        if (!content) {
            content = {
                items: this.getRandomItems(),
                effects: this.getRandomEffects()
            };
        }

        ModalManager.show('#mystery-merchant-modal', {
            onShow: (modal) => {
                modal.querySelector('.merchant-items').innerHTML = `
                    <div class="merchant-section">
                        <h3>神秘物品</h3>
                        <div class="item-list">
                            ${content.items.map(item => `
                                <div class="merchant-item">
                                    <div class="item-info">
                                        <h4>${item.name}</h4>
                                        <p>${item.description}</p>
                                        <span class="item-price">💰 ${Math.floor(item.price * 1.5)}</span>
                                    </div>
                                    <button class="buy-btn" data-type="item" data-id="${item.id}">购买</button>
                                </div>
                            `).join('')}
                        </div>
                    </div>
                    <div class="merchant-section">
                        <h3>神秘效果</h3>
                        <div class="effect-list">
                            ${content.effects.map(effect => `
                                <div class="merchant-item">
                                    <div class="item-info">
                                        <h4>${effect.name}</h4>
                                        <p>${effect.description}</p>
                                        <span class="item-price">💰 ${effect.price}</span>
                                    </div>
                                    <button class="buy-btn" data-type="effect" data-id="${effect.id}">购买</button>
                                </div>
                            `).join('')}
                        </div>
                    </div>
                `;

                // 添加购买事件
                modal.querySelectorAll('.buy-btn').forEach(btn => {
                    btn.onclick = () => {
                        const type = btn.dataset.type;
                        const id = btn.dataset.id;
                        if (type === 'item') {
                            this.buyItem(character, id, content.items);
                        } else {
                            this.buyEffect(character, id, content.effects);
                        }
                    };
                });
            }
        });
    }

    static getRandomItems(count = 3) {
        // 从商店物品池中随机选择，价格提高50%
        const items = [...ShopItems];
        const selected = [];
        
        while (selected.length < count && items.length > 0) {
            const index = Math.floor(Math.random() * items.length);
            selected.push(items.splice(index, 1)[0]);
        }
        
        return selected;
    }

    static getRandomEffects(difficulty = 'NORMAL', count = 2) {
        const pool = EffectPool[difficulty];
        const effects = [];
        const totalWeight = pool.reduce((sum, item) => sum + item.weight, 0);

        while (effects.length < count) {
            const rand = Math.random() * totalWeight;
            let weightSum = 0;
            
            for (const item of pool) {
                weightSum += item.weight;
                if (rand <= weightSum) {
                    effects.push(new item.effect());
                    break;
                }
            }
        }

        return effects;
    }

    static buyItem(character, itemId, items) {
        const item = items.find(i => i.id === itemId);
        if (!item) return;
        
        const price = Math.floor(item.price * 1.5); // 神秘商人的物品价格提高50%
        
        if (character.gold >= price) {
            character.gold -= price;
            character.inventory.push(item);
            NotificationManager.success(`购买了 ${item.name}`);
            ModalManager.hide('.modal');
        } else {
            NotificationManager.error('金币不足！');
        }
    }

    static buyEffect(character, effectId, effects) {
        const effect = effects.find(e => e.id === effectId);
        if (!effect) return;
        
        if (character.gold >= effect.price) {
            character.gold -= effect.price;
            effect.apply(character);
            character.effects = character.effects || new Set();
            character.effects.add(effect);
            NotificationManager.success(`获得了效果：${effect.name}`);
            ModalManager.hide('.modal');
        } else {
            NotificationManager.error('金币不足！');
        }
    }
} 